﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.SyncManage
{
    public partial class ComponentInfo : BasePage
    {
        private SysDictionaryBll sysBll = new SysDictionaryBll();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initForm();
            }
        }

        private void initForm()
        {
            if (Request.QueryString["Param"] != null && Request.QueryString["Param"] != "")
            {
                WsmComponentEntity sysDicEntity = sysBll.Get<WsmComponentEntity>(new Guid(Request.QueryString["Param"].ToString()));
                ComponentName.Text = sysDicEntity.ComponentName;
                ComponentDesc.Text = sysDicEntity.ComponentDesc;
                txtparmGUID.Value = Request.QueryString["Param"].ToString();               
            }
        }

        private bool save(ref string message)
        {
            WsmComponentEntity sysDicEntity;
            if (Request.QueryString["Param"] != null && Request.QueryString["Param"] != "")
            {
                sysDicEntity = sysBll.Get<WsmComponentEntity>(new Guid(txtparmGUID.Value));
            }
            else
            {
                sysDicEntity = new WsmComponentEntity();
                sysDicEntity.CreatedBy = CurrentUserID;
                sysDicEntity.CreatedOn = System.DateTime.Now;
                sysDicEntity.Deleted = false;
            }
            sysDicEntity.ComponentName = ComponentName.Text.Trim();
            sysDicEntity.ComponentDesc = ComponentDesc.Text.Trim();
            sysBll.InsertOrUpdate(sysDicEntity);

            return true;
        }

        protected void lnkbtnSaveAndNew_Click(object sender, EventArgs e)
        {
            string Message = "";
            if (!save(ref Message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "saveansrror", "alert('" + Message + "');", true);
            }
            else
            {
                ComponentName.Text = "";
                txtparmGUID.Value = "";
                ComponentDesc.Text = "";
                ClientScript.RegisterStartupScript(this.GetType(), "saveandnew", "alert('保存成功');", true);
            }
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            string Message = "";
            if (!save(ref Message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "saveansrror", "alert('" + Message + "');", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');window.close();", true);
            }
        }
    }
}
